package com.yiwei.hdb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.hdb.R;
import com.yiwei.hdb.api.Api;
import com.yiwei.hdb.api.bean.CheckPhoneRegisterBean;
import com.yiwei.hdb.util.Network;
import com.yiwei.hdb.util.RxLifeUtil;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.util.UI;
import com.yiwei.hdb.util.Util;
import com.yiwei.hdb.view.CircularWhiteProgress;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V4LoginTotalActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.btn_clear)
    ImageView btnClear;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.btn_wechat)
    TextView btnWechat;
    @BindView(R.id.loading_white)
    CircularWhiteProgress loadingWhite;
    @BindView(R.id.loading_success)
    ImageView loadingSuccess;
    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_total_v4);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        setIsNeedCheckLogout(false);
    }

    @OnTextChanged(R.id.edit_phone)
    void afterTextChanged(Editable s) {
        if (UI.toString(editPhone).length() == 11) {
            btnSubmit.setSelected(true);
            btnSubmit.setTextColor(0xffffffff);
        } else {
            btnSubmit.setSelected(false);
            btnSubmit.setTextColor(0xff999999);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    public void getCheckPhone(String mobile) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }
        loadingWhite.setVisibility(View.VISIBLE);
        btnSubmit.setText("");
        CheckPhoneRegisterBean bean = new CheckPhoneRegisterBean();
        bean.account = mobile;
        Api.api_service.getCheckPhoneRegister(bean).compose(RxLifeUtil.NocheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingWhite.setVisibility(View.GONE);
                    } catch (NullPointerException e) {

                    }

                }).
                subscribe(result -> {
                    if (result.getStatus() == 1) {
                        ToastUtil.makeText(this, "已发送验证码，请注意查收");
                        loadingSuccess.setVisibility(View.VISIBLE);
                        editPhone.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (result.data.has_register == 0) {
                                    Intent intent = new Intent(V4LoginTotalActivity.this, V4RegisterActivity.class);
                                    intent.putExtra("phone", mobile);
                                    intent.putExtra("invite_code", result.data.invite_code);
                                    startActivity(intent);
                                } else if (result.data.has_register == 1) {
                                    Intent intent = new Intent(V4LoginTotalActivity.this, V4LoginByCodeActivity.class);
                                    intent.putExtra("phone", mobile);
                                    startActivity(intent);
                                }
                                finish();
                            }
                        }, 2000);
                    } else {
                        ToastUtil.makeText(this, result.getMsg());
                        btnSubmit.setText("获取验证码");
                    }


                });
    }


    @OnClick({R.id.btn_back, R.id.btn_clear, R.id.btn_submit, R.id.btn_wechat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_clear:
                editPhone.setText("");
                break;
            case R.id.btn_submit:
                if (editPhone.length() == 11) {
                    getCheckPhone(UI.toString(editPhone));
                } else {
                    ToastUtil.makeText(this, "请正确填写手机号");
                }
                break;
            case R.id.btn_wechat:
                Util.startActivity(this, WechatChatActivity.class);
                break;
        }
    }
}
